package com.buydeem.singleton;

/**
 * 单例模式
 *
 * @author zengchao
 * @date 2022-04-26 12:01:01
 */
public class FileManager {
    private static volatile FileManager instance;

    public FileManager() {
    }

    /**
     * 线程不安全
     *
     * @return
     */
    @Deprecated
    public static FileManager getInstance() {
        if (instance == null) {
            instance = new FileManager();
        }
        return instance;
    }

    /**
     * 使用同步方法解决单例线程安全
     *
     * @return
     */
    public static synchronized FileManager getInstanceWithSyncLock() {
        if (instance == null) {
            instance = new FileManager();
        }
        return instance;
    }

    /**
     * 双重检查锁定解决单例线程安全
     *
     * @return
     */
    public static FileManager getInstanceWithDCL() {
        if (instance == null) {
            synchronized (FileManager.class) {
                if (instance == null) {
                    instance = new FileManager();
                }
            }
        }
        return instance;
    }


    public static void main(String[] args) {
        //线程不安全
        FileManager fileManager1 = FileManager.getInstance();
        FileManager fileManager2 = FileManager.getInstance();
        System.out.println(fileManager1 == fileManager2);

    }
}
